# coding=utf-8
# encoding=utf-8
from sqlalchemy import Integer, String, DateTime, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import *
from datetime import datetime
from DataBase.Connect import session

# Model基类,创建了一个BaseModel类,这个类的子类可以自动与一个表关联
Base = declarative_base()
# 这样设置以后直接可以通过model类进行query查询,否则必须通过session进行查询。
Base.query = session.query_property()

# 定义数据库类
class ACStatus(Base):
    #表的名字
    __tablename__ = 'Status_Tabel'
    #表的结构
    DeviceAddress=Column(String(20),primary_key=True)
    ATemp = Column(String(20))
    Aoc = Column(String(20))
    Dwind = Column(String(20))
    FModel = Column(String(20))
    HElec = Column(String(20))
    ErrorMessage = Column(String(20))
    Updatetime = Column(String(20))
    Aoctime = Column(String(20))
    eTemp = Column(String(20))
    def __str__(self):
        pass

    def __init__(self,
                 DeviceAddress=None,ATemp=None,Aoc=None,
                 Dwind=None,FModel=None,HElec=None,ErrorMessage=None,
                 Updatetime=None,Aoctime=None,eTemp=None
                 ):
        self.ATemp=ATemp
        self.DeviceAddress=DeviceAddress
        self.Aoc=Aoc
        self.Dwind=Dwind
        self.FModel=FModel
        self.HElec=HElec
        self.Updatetime=Updatetime
        self.ErrorMessage=ErrorMessage
        self.Aoctime=Aoctime
        self.eTemp=eTemp

class ACStatusWater(Base):
    # 表的名字
    __tablename__ = 'StatusWater'
    # 表的结构
    id = Column(String(20), primary_key=True)
    DeviceAddress = Column(String(20))
    ATemp = Column(String(20))
    Aoc = Column(String(20, convert_unicode=True))
    Dwind = Column(String(20))
    FModel = Column(String(20))
    HElec = Column(String(20))
    ErrorMessage = Column(String(20))
    Updatetime = Column(String(20))
    eTemp = Column(String(20))
    Aoctime = Column(String(20))

    def __str__(self):
        pass

    def __init__(self,id=None,
                 DeviceAddress=None,ATemp=None,Aoc=None,
                 Dwind=None,FModel=None,HElec=None,ErrorMessage=None,
                 Updatetime=None,Aoctime=None,eTemp=None
                 ):
        self.id = id
        self.ATemp = ATemp
        self.DeviceAddress = DeviceAddress
        self.Aoc = Aoc
        self.Dwind = Dwind
        self.FModel = FModel
        self.HElec = HElec
        self.Updatetime = Updatetime
        self.ErrorMessage = ErrorMessage
        self.Aoctime = Aoctime
        self.eTemp = eTemp


class ACInfo(Base):
    # 表的名字
    __tablename__ = 'Set_Tabel'
    # 表的结构
    DeviceID = Column(String(20), primary_key=True)
    DeviceAddress = Column(String(20))
    DevicePort = Column(String(20))
    def __init__(self,DeviceID=None,DeviceAddress=None,DevicePort=None):
        self.DeviceID=DeviceID
        self.DeviceAddress=DeviceAddress
        self.DevicePort=DevicePort


class ACControl(Base):
    # 表的名字
    __tablename__ = 'Control_Tabel'
    # 表的结构
    id = Column(Integer,Sequence('id_seq'), primary_key=True)
    Aoc = Column(String(20))
    ATemp = Column(String(20))
    DeviceID = Column(String(20))
    Dwind = Column(String(20))
    FModel = Column(String(20))
    HElec = Column(String(20))
    status = Column(String(20))
    Updatetime = Column(String(20))
    randomid = Column(String(20))
    def __init__(self,
                 randomid=u"0",Aoc=None,ATemp=None,DeviceID=0,
                 Dwind=None,FModel=None,HElec=None,
                 status=None,Updatetime=None,
                 ):
        self.randomid = randomid
        self.ATemp=ATemp
        self.Aoc=Aoc
        self.Dwind=Dwind
        self.FModel=FModel
        self.HElec=HElec
        self.Updatetime=Updatetime
        self.DeviceID=DeviceID
        self.status=status

    # 将json字符串转换为ACStatus，传入为一个json字符串
    @classmethod
    def Json2ModelList(self,obj):
        returnedList=[]
        for row in obj:
            returnedList.append(
                ACControl(randomid=row['randomId'],
                          ATemp=row['atemp'].encode('gbk'),
                          Aoc=row['aoc'],
                          Dwind=row['dwind'],
                          FModel=row['fmodel'],
                          HElec=row['helec'],
                          Updatetime=u'-1',
                          DeviceID=row['deviceId'],
                          status=u'未发送'
                         )
            )
        return returnedList


class ACSend(object):
    #
    # deviceId = None
    # atemp = None
    # aoc = None
    # dwind = None
    # helec = None
    # fmodel = None
    # etemp = None
    # errorMsg = None
    # aocTime = None
    # updateTime = None
    def __init__(self,ACStatusWater,ACInfo):
        # self.id=None
        # self.id=ACStatusWater.Id
        self.deviceId = ACInfo.DeviceID
        if  ACStatusWater.ATemp is u'':
            self.atemp='-99'
        else:
            self.atemp = ACStatusWater.ATemp
        self.aoc = ACStatusWater.Aoc
        self.dwind = ACStatusWater.Dwind
        self.helec = ACStatusWater.HElec
        self.fmodel = ACStatusWater.FModel
        if  ACStatusWater.eTemp is u'':
            self.etemp='-99'
        else:
            self.etemp = ACStatusWater.eTemp
        self.errorMsg = ACStatusWater.ErrorMessage
        self.updateTime = ACStatusWater.Updatetime
        self.aocTime=ACStatusWater.Aoctime
        self.id=ACStatusWater.id
    # def encoding(self):

        # self.status=None
        # self.sendinstance={"deviceId":self.deviceId,
        #                "atemp": self.atemp,
        #                "aoc": self.aoc,
        #                "dwind": self.dwind,
        #                "helec": self.helec,
        #                "fmodel": self.fmodel,
        #                "etemp": self.etemp,
        #                "errorMsg": self.errorMsg,
        #                "aocTime": self.aocTime,
        #                "updateTime": self.updateTime,
        #                }


class ACCrlSend(object):
    def __init__(self,ACControl):
        self.randomId = ACControl.randomid
        self.deviceId = ACControl.DeviceID
        self.atemp = ACControl.ATemp
        self.aoc = ACControl.Aoc
        self.dwind = ACControl.Dwind
        self.fmodel = ACControl.FModel
        self.helec = ACControl.HElec
        self.status = ACControl.status
        self.updateTime = ACControl.Updatetime
        self.id = ACControl.id


class Meta(object):
    def __init__(self,success=None,message=None):
        self.success=success
        self.message=message

    def isSuccess(self):
        return self.success

    def getMessage(self):
        return self.message


class Response(object):
    def __init__(self,inmeta=Meta(),indata=None):
        # self.meta.success=inmeta['success']
        # self.meta.message=inmeta['message']
        self.meta = inmeta
        self.data = indata

    def success(self,data=None):
        self.meta = Meta(True, 'OK')
        if data is not None:
            self.data=data
        return self

    def failure(self,failmessage=None):
        if failmessage is not None:
            self.meta=Meta(False,message=failmessage)
        else:
            self.meta=Meta(False,message='error')
        return self

    @staticmethod
    def Json2ModelList(obj):
        # type: (object) -> object
        '''
        解析传入的response数据结构体
        :param obj: json型的response数据
        :return: 解析之后的response实例
        '''
        return Response(inmeta=Meta(obj['meta']['success'], obj['meta']['message']),
                 indata=obj['data'])